package com.park.service.impl.user;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.park.client.model.user.UserMsgModel;
import com.park.client.model.user.UserMsgStatusModel;
import com.park.client.service.user.UserMsgService;
import com.park.common.enums.ReadMsgStatusEnum;
import com.park.dao.user.UserMsgMapper;
import com.park.dao.user.UserMsgStatusMapper;
import com.park.service.impl.base.BaseServiceImpl;

@Service("userMsgService")
public class UserMsgServiceImpl extends BaseServiceImpl<UserMsgModel> implements UserMsgService {
	private static final Logger logger =LoggerFactory.getLogger(UserMsgServiceImpl.class);
	@Autowired
	private  UserMsgMapper  userMsgMapper;
	@Autowired
	private  UserMsgStatusMapper  userMsgStatusMapper;
	
	@Override
	public boolean deleteByPrimaryKey(Long id) {
		return userMsgMapper.deleteByPrimaryKey(id)==1;
	}
	@Override
	public boolean insertSelective(UserMsgModel record) {
		return userMsgMapper.insertSelective(record)==1;
	}
	@Override
	public UserMsgModel selectByPrimaryKey(Long id) {
		return userMsgMapper.selectByPrimaryKey(id);
	}
	@Override
	public boolean updateByPrimaryKeySelective(UserMsgModel record) {
		return userMsgMapper.updateByPrimaryKeySelective(record)==1;
	}
	@Override
	public boolean updateByPrimaryKey(UserMsgModel record) {
		return userMsgMapper.updateByPrimaryKey(record)==1;
	}
	@Override
	public boolean insert(UserMsgModel record) {
		return userMsgMapper.insert(record)==1;
	}
	@Override
	public int getMsgTotal(Long userId) {
		return userMsgMapper.getMsgTotal(userId);
		
	}
	@Override
	public List<UserMsgModel> selectAll() {
		return userMsgMapper.selectAll();
	}
	@Override
	public boolean updateByPrimaryKeyWithBLOBs(UserMsgModel record) {
		return false;
	}
	@Override
	public boolean insertAndGetInsertID(UserMsgModel msg) {
		return userMsgMapper.insertAndGetInsertID(msg)==1;
	}
	@Override
	public List<UserMsgModel> selectPrivateMsg(Long userId) {
		return userMsgMapper.selectPrivateMsg(userId);
	}
	@Override
	public List<UserMsgModel> selectPagination(UserMsgModel t) {
		return userMsgMapper.selectPagination(t);
	}
	
	@Override
	public Integer selectTotal(UserMsgModel t) {
		return userMsgMapper.selectTotal(t);
	}
	
	@Override
	public void saveMsg(UserMsgModel msg,Long userId) {
		try {
			msg.setCreateDate(new Date());
			this.insertAndGetInsertID(msg);
			UserMsgStatusModel userMsg = new UserMsgStatusModel();
			userMsg.setMsgId(msg.getId());
			userMsg.setCreater(userId);
			userMsg.setStatus(ReadMsgStatusEnum.NOT_READ.code);//未读
			userMsgStatusMapper.insertSelective(userMsg);
		} catch (Exception e) {
			logger.error("注册发送消息失败",e);
		}
	}
	

}
